package com.gz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.dao.PaperQuestionMapper;
import com.gz.dto.PageData;
import com.gz.dto.PaperQuestionDto;
import com.gz.entity.PaperQuestion;
import com.gz.entity.TestPaper;
import com.gz.service.PaperQuestionService;
import java.util.Date;
import java.util.List;

import com.gz.service.TestPaperService;
import com.gz.vo.R;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * (PaperQuestion)表服务实现类
 *
 * @author makejava
 * @since 2024-02-26 11:20:17
 */
@Service
public class PaperQuestionServiceImpl implements PaperQuestionService {

  @Autowired
  private PaperQuestionMapper paperQuestionDao;
  @Autowired
  private TestPaperService testPaperService;

  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Override
  public PaperQuestion queryById(Integer id) {
    return paperQuestionDao.queryById(id);
  }

  /**
   * 分页查询
   *
   * @param dto 筛选条件
   * @return 查询结果
   */
  @Override
  public PageData queryByPage(PaperQuestionDto dto) {
    PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
    List<PaperQuestion> list = paperQuestionDao.queryAll(dto);
    PageInfo<PaperQuestion> pageInfo = new PageInfo<>(list);
    return new PageData<>(pageInfo.getTotal(), list);
  }

  /**
   * 分页查询
   *
   * @param dto 筛选条件
   * @return 查询结果
   */
  @Override
  public List<PaperQuestion> queryAll(PaperQuestionDto dto) {
    List<PaperQuestion> list = paperQuestionDao.queryAll(dto);
    return list;
  }

  @Override
  public Long count(PaperQuestion param) {
    return paperQuestionDao.count(param);
  }

  /**
   * 新增数据
   *
   * @param paperQuestion 实例对象
   * @return 实例对象
   */
  @Override
  public PaperQuestion insert(PaperQuestion paperQuestion) {
    paperQuestionDao.insert(paperQuestion);
    return paperQuestion;
  }
  @Override
  @Transactional
  public R<?> saveData(PaperQuestion paperQuestion) {
    if (StringUtils.isEmpty(paperQuestion.getPaperId())) {
      return R.failure("试卷id不能为空");
    }
    String paperId = paperQuestion.getPaperId();
    /** 先查是否已经录满 */
    TestPaper testPaper = testPaperService.queryById(Integer.parseInt(paperId));
    PaperQuestionDto dto = new PaperQuestionDto();
    dto.setPaperId(paperId);
    List<PaperQuestion> existQt = this.queryAll(dto);
    if (CollectionUtils.isNotEmpty(existQt)) {
      for (PaperQuestion a : existQt) {
        if (a.getQuestionId().equals(paperQuestion.getQuestionId())) {
          return R.failure("此题目:" + a.getQuestionId() + "已经录入过");
        }
      }
    }
    if (null == testPaper.getEnteredQtCnt()) {
      testPaper.setEnteredQtCnt(0);
    }
    if (testPaper.getEnteredQtCnt() >= testPaper.getQuestionCount()) {
      return R.failure("您的试卷题目已经录满");
    }
    PaperQuestion insert = this.insert(paperQuestion);
    TestPaper paperParam = new TestPaper();
    paperParam.setId(testPaper.getId());
    /** 录入题目后，剩余题目数，自然就减1 */
    paperParam.setLeftPosCnt(testPaper.getLeftPosCnt() - 1);
    paperParam.setEnteredQtCnt(testPaper.getEnteredQtCnt() + 1);
    testPaperService.update(paperParam);
    return R.ok(insert);
  }

  /**
   * 修改数据
   *
   * @param paperQuestion 实例对象
   * @return 实例对象
   */
  @Override
  public PaperQuestion update(PaperQuestion paperQuestion) {
    paperQuestionDao.update(paperQuestion);
    return queryById(paperQuestion.getId());
  }

  /**
   * 批量修改数据
   *
   * @param list 实例对象
   * @return 实例对象
   */
  @Override
  public int updateBatch(List<PaperQuestion> list) {
    return paperQuestionDao.updateBatch(list);
  }

  /**
   * 批量修改数据
   *
   * @param list 实例对象
   * @return 实例对象
   */
  @Override
  public int insertBatch(List<PaperQuestion> list) {
    if (CollectionUtils.isNotEmpty(list)) {
     for(PaperQuestion a:list){
        a.setCrtDt(new Date());
        a.setOptDt(new Date());
      }
      return paperQuestionDao.insertBatch(list);
    } else {
      return 0;
    }
  }
  /**
   * 批量修改数据
   *
   * @param list 实例对象
   * @return 实例对象
   */
  @Override
  public R<?> saveBatch(List<PaperQuestion> list) {
    return null;
  }

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 是否成功
   */
  @Override
  public boolean deleteById(Integer id) {
    return paperQuestionDao.deleteById(id) > 0;
  }
  @Override
  public boolean deleteByPaperId(Integer paperId) {
    return paperQuestionDao.deleteByPaperId(paperId) > 0;
  }
  @Override
  public boolean deleteByPaperIds(List<Integer> paperIds) {
    return paperQuestionDao.deleteByPaperIds(paperIds) > 0;
  }
}
